package com.echo.fm.content.news.type;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.echo.fm.content.news.type.model.NewsType;
import com.echo.util.ErrorException;
import com.echo.util.ResultUtils;

@Repository("newsTypePersistence")
public class DaoNewsTypePersistenceImpl implements INewsTypePersistence {
	@Autowired
	@Qualifier("sessionFactory")
	SessionFactory sessionFactory;

	public void addNewsType(NewsType newsType) throws ErrorException {
		Session session = null;
		Transaction transaction = null;
		try {
			session = sessionFactory.openSession();
			transaction = session.beginTransaction();
			session.save(newsType);
			transaction.commit();
		} catch (Exception e) {
			transaction.rollback();
			e.printStackTrace();
			throw new ErrorException(ResultUtils.WEB_DB_SERVER_ERROR_CODE);
		} finally {
			if (session != null)
				session.close();

		}

	}

	public List<NewsType> searchNewsTypeList() throws ErrorException {
		Session session = null;
		try {
			session = sessionFactory.openSession();
			@SuppressWarnings("unchecked")
			List<NewsType> newsTypeList = session.createQuery("from NewsType where 1=1").list();
			return newsTypeList;
		} catch (HibernateException e) {
			e.printStackTrace();
			throw new ErrorException(ResultUtils.WEB_DB_SERVER_ERROR_CODE);
		} finally {
			if (session != null)
				session.close();
		}

	}

	public void updateNewsType(NewsType newsType) throws ErrorException {
		Session session = null;
		Transaction tx = null;
		try {
			session = sessionFactory.openSession();
			tx = session.beginTransaction();
			session.update(newsType);
			tx.commit();
		} catch (HibernateException e) {
			tx.rollback();
			e.printStackTrace();
			throw new ErrorException(ResultUtils.WEB_DB_SERVER_ERROR_CODE);
		} finally {
			if (session != null)
				session.close();
		}

	}

	public void deleteNewsType(NewsType newsType) throws ErrorException {
		Session session = null;
		Transaction tx = null;
		try {
			session = sessionFactory.openSession();
			tx = session.beginTransaction();
			session.delete(newsType);
			tx.commit();
		} catch (HibernateException e) {
			tx.rollback();
			e.printStackTrace();
			throw new ErrorException(ResultUtils.WEB_DB_SERVER_ERROR_CODE);
		} finally {
			if (session != null)
				session.close();
		}

	}

	public NewsType searchNewTypeId(Integer id) throws ErrorException {
		Session session = null;
		try {
			session = sessionFactory.openSession();
			return (NewsType) session.get(NewsType.class, id);
		} catch (HibernateException e) {
			e.printStackTrace();
			throw new ErrorException(ResultUtils.WEB_DB_SERVER_ERROR_CODE);
		} finally {
			if (session != null)
				session.close();
		}

	}

}
